package com.crane.common.framework.shiro;

import org.apache.commons.lang3.SerializationException;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;


/**
 * zhenxing.pi
 *
 * @param <T>
 */
public class JdkRedisSerializer<T extends Serializable> implements RedisSerializer<T> {

    private JdkRedisSerializer() {
    }

    public static volatile JdkRedisSerializer<Serializable> jdkRedisSerializer = null;

    public static JdkRedisSerializer<Serializable> getInstance() {
        if (jdkRedisSerializer == null) {
            synchronized (JdkRedisSerializer.class) {
                if (jdkRedisSerializer == null) {
                    jdkRedisSerializer = new JdkRedisSerializer<>();
                }
            }
        }
        return jdkRedisSerializer;
    }

    @SuppressWarnings("restriction")
    @Override
    public byte[] serialize(T t) throws SerializationException {
        return SerializationUtils.serialize(t);
    }

    @SuppressWarnings("restriction")
    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        return SerializationUtils.deserialize(bytes);

    }
}
